var $;
layui.config({
    base : "../../js/"
}).use(['form','layer','jquery','laytpl'],function(){
    var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		 laypage = layui.laypage,
         laytpl = layui.laytpl,
		 $ = layui.jquery;

    $.get("/shiro/role/select", function(r){
        var getTpl = role.innerHTML
            ,view = document.getElementById('view');
           laytpl(getTpl).render(r, function(html){
            view.innerHTML = html;
        });
        form.render(null, 'formresh');
    });

 	var addUserArray = [],addUser,roleIdList=[];
 	form.on("submit(addUser)",function(data){
 		//是否添加过信息
	 	if(window.sessionStorage.getItem("addUser")){
	 		addUserArray = JSON.parse(window.sessionStorage.getItem("addUser"));
	 	}
        $("input:checkbox[name='poetrytype']:checked").each(function(){
            roleIdList.push($(this).val());
        });
 		addUser = '{"nickname":"'+ $(".nickName").val() +'",';  //登录名
 		addUser += '"username":"'+ $(".userName").val() +'",';	 //用户名
        addUser += '"email":"'+ $(".userEmail").val() +'",';	 //邮箱
 		addUser += '"password":"'+ $(".password").val() +'",'; //密码
 		addUser += '"status":"'+ data.field.userStatus +'",'; //状态
 		addUser += '"roleIdList":"'+  roleIdList +'"}';  //角色
 		console.log(addUser);
 		addUserArray.unshift(JSON.parse(addUser));
 		window.sessionStorage.setItem("addUser",JSON.stringify(addUserArray));
 		//弹出loading
 		var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.ajax({
            type: "POST",
            url:   "/shiro/user/adduser",
            contentType: "application/json",
            data: JSON.stringify(JSON.parse(addUser)),
            success: function(r){
                if(r.code === 0){

                        top.layer.msg("用户添加成功！");

                }else{
                    alert(r.msg);
                }
            }
        });
        top.layer.close(index);
        layer.closeAll("iframe");
        //刷新父页面
        parent.location.reload();
 		return false;
 	})
	
})

//格式化时间
function formatTime(_time){
    var year = _time.getFullYear();
    var month = _time.getMonth()+1<10 ? "0"+(_time.getMonth()+1) : _time.getMonth()+1;
    var day = _time.getDate()<10 ? "0"+_time.getDate() : _time.getDate();
    var hour = _time.getHours()<10 ? "0"+_time.getHours() : _time.getHours();
    var minute = _time.getMinutes()<10 ? "0"+_time.getMinutes() : _time.getMinutes();
    return year+"-"+month+"-"+day+" "+hour+":"+minute;
}
